<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta charset="utf-8">
    <title>deepCopy.js - Documentation</title>
    
    
    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
    <script src="scripts/nav.js" defer></script>
    
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav >
    
    
    <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#debounce">debounce</a></li><li><a href="global.html#deepCopy">deepCopy</a></li></ul>
    
</nav>

<div id="main">
    
    <h1 class="page-title">deepCopy.js</h1>
    

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>
/**
 * @description 深拷贝
 * @param {any} obj 拷贝目标
 * @returns {object} 拷贝结果
 */
export function deepCopy(obj, memo = new WeakMap()) {
	// 检查是否是基本数据类型或者是函数，如果是则直接返回
	if (obj === null || typeof obj !== 'object' || typeof obj === 'function') {
		return obj;
	}

	// 检查是否已经拷贝过该对象，避免循环引用问题
	if (memo.has(obj)) {
		return memo.get(obj);
	}

	// 根据原始对象的类型创建一个新的对象或数组
	const copy = Array.isArray(obj) ? [] : {};

	// 将新对象添加到 WeakMap 中
	memo.set(obj, copy);

	// 遍历原始对象的属性或元素，递归进行深拷贝
	for (const key in obj) {
		if (Object.prototype.hasOwnProperty.call(obj, key)) {
			copy[key] = deepCopy(obj[key], memo);
		}
	}

	return copy;
}</code></pre>
        </article>
    </section>




    
    
</div>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Tue Sep 12 2023 13:19:40 GMT+0800 (中国标准时间) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>



</body>
</html>
